<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../react18/react.development.js"></script>
    <script src="../react18/react-dom.development.js"></script>
    <script src="../react18/babel.min.js"></script>
</head>
<body>
    <button id="btn">添加水果</button>
    <div id="app"></div>

    <script type="text/babel">

        const arr = ['西瓜','菠萝','水蜜桃','橘子']
        //创建虚拟DOM
        const vDOM = (
            <div>
                <h1>水果名</h1>
                <ul>
                  {
                    arr.map(item=>(<li>{item}</li>))
                  }
                </ul>
            </div>
        )

        const root = ReactDOM.createRoot(document.getElementById('app'))
        root.render(vDOM)
        
        // 给按钮绑定点击事件
        const oBtn = document.getElementById('btn');   
        oBtn.onclick = function(){
            //给数组末尾添加一个值
            arr.unshift('苹果')
            const vDOM = (
            <div>
                <h1>水果名</h1>
                <ul>
                  {
                    arr.map(item=>(<li>{item}</li>))
                  }
                </ul>
            </div>
        )
        root.render(vDOM)
        }

        //如果往头部新增，则会从新排列顺序，导致全部都会从新对比渲染。
    </script>
</body>
</html>